#!/bin/sh
set -e

version=$1

if [ -z "$version" ] ; then
  echo "Usage: ./release 7.7.0"
  exit 0
fi

if [ ! -d "../selenide-web" ]
then
    echo "Directory ../selenide-web doesn't exist"
    exit 1
fi

branch=$(git branch --show-current)
if [ "$branch" != "main" ]
then
    echo "Current branch is not 'main': '$branch'"
    exit 1
fi

echo "Releasing version: ${version}"

./gradlew clean check javadocForSite
./gradlew publishToMavenCentral --no-parallel --info

git tag -a "v${version}" -m "released selenide ${version}"
git push origin --tags

mkdir "../selenide-web/_site/javadoc/${version}"
cp -r build/javadoc-for-site/. "../selenide-web/_site/javadoc/${version}/"
rm -fr "../selenide-web/_site/javadoc/current/*"
cp -r build/javadoc-for-site/. ../selenide-web/_site/javadoc/current/
cd ../selenide-web || exit 2
git add "_site/javadoc/${version}"
git add "_site/javadoc/current"
git commit -m "publish javadoc for Selenide ${version}"
git push
cd - || exit 3
